package com.sjm.bookshop.controller.client;

import com.sjm.bookshop.entity.CountOfMonth;
import com.sjm.bookshop.entity.Orders;
import com.sjm.bookshop.entity.User;
import com.sjm.bookshop.service.client.OrderService;
import com.sjm.bookshop.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderCtrl {


    @Autowired
    private OrderService orderService;


    /**
     * 用户个人订单
     *
     * @param uId
     * @return
     */
    @RequestMapping("/getOrderByUserId/{uId}")
    public Result getUserOrders(@PathVariable("uId") int uId) {
        List<Orders> orderList = orderService.orders(uId);
        return Result.success(200, orderList, "success");
    }


    /**
     * 确认收货
     *
     * @param orderFId
     * @return
     */
    @RequestMapping("/ConfirmOfGoods/{orderFId}/{orderStatus}")
    public Result ConfirmOfGoods(@PathVariable String orderFId,@PathVariable int orderStatus) {
        if(orderStatus==3){
            int result = orderService.ConfirmOrder(orderFId);
            if (result > 0) {
                return Result.success(200, "已确认收货");
            } else {
                return Result.fail(201, "确认收货失败");
            }
        }else {
            return Result.fail(202, "收货失败");
        }

    }

    /**
     * 用户根据Id删除已经完成的订单
     *
     * @param orderFId
     * @return
     */
    @RequestMapping("/delOrderOfGoods/{orderFId}")
    public Result delOrderOfGoods(@PathVariable String orderFId) {
        int result = orderService.delOrder(orderFId);
        if (result == 1) {
            return Result.success(200, "已删除一条订单记录");
        } else {
            return Result.fail(201, "删除失败");
        }
    }
//  订单管理

    /**
     * 所有用户订单
     *
     * @return
     */
    @RequestMapping("/getAllOrder")
    public Result getUserOrders() {
        List<User> userList = orderService.getOrdersList();
        return Result.success(200, userList, "success");
    }

    /**
     * 提交快递信息
     *
     * @param orders
     * @return
     */
    @RequestMapping("/sendGoods")
    public Result sendOrder(@RequestBody Orders orders) {
        System.out.println(orders.toString());
        int result = orderService.update(orders);
        if (result > 0) {
            return Result.success(200, "发货成功");
        } else {
            return Result.fail(201, "发货失败");
        }
    }

    @RequestMapping("/delOrder")
    public Result delOrder(String orderFId, int adminstatus, int jurisdiction) {

        if (adminstatus == 0 && jurisdiction == 1) {
            int result = orderService.delOrder(orderFId);
            if (result > 0) {
                return Result.success(200, "删除成功");
            } else {
                return Result.fail(201, "删除失败");
            }
        } else {
            return Result.fail(201, "删除失败");
        }
    }

    /**
     * 月收入数据
     * @return
     */
    @RequestMapping("/dataBoard")
    public Result dateBord() {
        List<CountOfMonth> board = orderService.getBoard();
        return Result.success(200, board, "success");
    }


    @RequestMapping("/datatwo")
    public Result getListByCountOrder(){
        List<CountOfMonth> listByCountOrder = orderService.getListByCountOrder();
        return Result.success(200, listByCountOrder, "success");
    }

    @RequestMapping("/datathree")
    public Result getListByCountOrderThreeDays(){
       Integer listByCountOrder = orderService.CountOrderThirtyDays();
        return Result.success(200, listByCountOrder, "success");
    }

    @RequestMapping("/daysdata")
    public Result getListByDays(){
        List<CountOfMonth> listBydays = orderService.getDaysCount();
        return Result.success(200, listBydays, "success");
    }


}
